package com.hamster.crm.web;
import com.hamster.crm.exception.ExceptionEnum;
import com.hamster.crm.exception.LyException;
import com.hamster.crm.pojo.Project;
import com.hamster.crm.pojo.ProjectCategory;
import com.hamster.crm.pojo.ProjectCourse;
import com.hamster.crm.service.ProjectService;
import com.hamster.crm.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * 项目管理的Controller
 * @author Tarry
 * @create 2019/12/24 14:45
 */
@RestController
@RequestMapping("project")
public class ProjectController {
    @Autowired
    private ProjectService projectService;

    /**
     * 获取项目分类【课程、vip、小分公司、大分公司】
     * @return
     */
    @GetMapping("listProjectCategory")
    public ResponseEntity<List<ProjectCategory>> listProjectCategory(){
        List<ProjectCategory> list = projectService.listProjectCategory();
        return ResponseEntity.ok(list);
    }

    /**
     * 新增项目
     * @param project
     * @return
     */
    @PostMapping("insert")
    public ResponseEntity<Void> insert(@RequestBody Project project){
        projectService.insert(project);
        return ResponseEntity.ok().build();
    }

    /**
     * 根据id获取项目详情
     * @param id
     * @return
     */
    @GetMapping("get")
    public ResponseEntity<Project> get(@RequestParam String id){
        Project project = projectService.get(id);
        return ResponseEntity.ok(project);
    }

    /**
     * 根据id删除项目
     * @param ids id 集合
     * @return
     */
    @PostMapping("delete")
    public ResponseEntity<Void> delete(@RequestBody List<String> ids){
        projectService.delete(ids);
        return ResponseEntity.ok().build();
    }

    /**
     * 修改项目
     * @param project 项目详情对象
     * @return 状态码 200
     */
    @PostMapping("update")
    public ResponseEntity<Void> update(@RequestBody Project project){
        projectService.update(project);
        return ResponseEntity.ok().build();
    }

    /**
     * 条件查询项目列表，分页
     * @param pageSize          每页条数
     * @param currentPage       当前页号
     * @param title             项目名称
     * @return PageResult<Project> 页面结果
     */
    @GetMapping("listProject")
    public ResponseEntity<PageResult<Project>> listProjects(
            @RequestParam(required=true) Integer pageSize,
            @RequestParam(required=true) Integer currentPage,
            @RequestParam(required = false) String title
    ){
        PageResult<Project> result = projectService.listProjects(pageSize,currentPage,title);

        return ResponseEntity.ok(result);
    }

    /**
     * 根据项目id，获取该项目所选择的 课程
     * @param id
     * @return
     */
    @GetMapping("listCourse")
    public ResponseEntity<List<ProjectCourse>> listCourses(@RequestParam String id){
        List<ProjectCourse> projectCourses = projectService.listCourse(id);
        return ResponseEntity.ok(projectCourses);
    }


}
